//需要学校ID 和用户ID
var schoolDetails = {
	page: 1,
	init: function() {
		this.TabStick();
		this.addEvent();
		this.GetSchoolInfo();
		this.getMajorList(schoolDetails.page);
	},
	//事件
	addEvent: function() {
		//NAV 导航
		//收藏学校
		$('#school_tilte').on('click', '.school-attention', function() {
			var _this = $(this);
			var setData = {
				'user_id': fnBase.splitURL()['user_id'],
				'id': fnBase.splitURL()['school_id']
			};
			fnBase.commonAjax(fnBase.api.School_InformCollect, setData, function(data) {
				if(data.code == 0) {
					_this.removeClass('school-attention').addClass('school-attention-no');
					_this.html('已收藏');
					fnBase.toast('收藏成功');
				}
			});
		});
		//取消收藏
		$('#school_tilte').on('click', '.school-attention-no', function() {
			var _this = $(this);
			var setData = {
				'user_id': fnBase.splitURL()['user_id'],
				'id': fnBase.splitURL()['school_id']
			};
			fnBase.commonAjax(fnBase.api.School_InformCollectCancel, setData, function(data) {
				if(data.code == 0) {
					_this.removeClass('school-attention-no').addClass('school-attention');
					_this.html('收藏');
					fnBase.toast('取消收藏');
				}
			});
		});
		//跳转专业详情
		$('#admission_list').bind('click').on('click', '.admission-li', function() {
			var data_id = $(this).attr('data_id');
			//fnBase.toast('跳转专业详情');
			if(fnBase.versions.ios || fnBase.versions.iPhone || fnBase.versions.iPad) {
				GotoMajorDetail(data_id);
			} else if(fnBase.versions.android) {
				window.myObj.toMajor(data_id);
			}
		});//跳转原生设置分数
		$('#matrGoNative').click(function(){
			if(fnBase.versions.ios || fnBase.versions.iPhone || fnBase.versions.iPad) {
					Go2SetInfo();
				} else if(fnBase.versions.android) {
					window.myObj.toSetMark();
				}
		});//跳转原生设置分数
		$('#myValue').click(function(){
			if(fnBase.versions.ios || fnBase.versions.iPhone || fnBase.versions.iPad) {
					Go2SetInfo();
				} else if(fnBase.versions.android) {
					window.myObj.toSetMark();
				}
		});
		//点击加载更多
		$('#admission_more').click(function() {
			schoolDetails.getMajorList(schoolDetails.page)
		});
	},
	//获取学校详情
	GetSchoolInfo: function() {
		var loadingMask = document.getElementById('cssload');
		$.ajax({
			type: "post",
			url: fnBase.api.School_CollegeInfo,
			timeout: 10000,
			data: {
				"user_id": fnBase.splitURL()['user_id'],
				"id": fnBase.splitURL()['school_id'],
			},
			dataType: "json",
			success: function(data) {
				console.log(data);
				if(data.code == 0) {
					//title
					var datal = data.data;
					var is985 = (datal.is985 == '是') ? '<li>985</li>' : '';
					var is211 = (datal.is211 == '是') ? '<li>211</li>' : '';
					var is_collect = (datal.is_collect == '1') ? '<li class="school-attention-no">已收藏</li>' : '<li class="school-attention">收藏</li>';
					var is_zhishu = (datal.is_zhishu == '1') ? '<li>教育部直属</li>' : '';
					var titleStr = '<li><h4>' + datal.name + '</h4><dd>' +
						is985 + is211 + is_zhishu +
						'</dd></li>' + is_collect;
					var ImgSrc=''+ fnBase.api.School_img + datal.id + '.jpg';
					$('#myImg').attr('src',ImgSrc);
					$('#school_tilte').append(titleStr);
					$('#school_prov').html(datal.prov_name);
					var wlStr = '',GuideStr = '',EmpStr = '';
					if(datal.wl == '1') {
						wlStr = '文科';
					} else if(datal.wl == '2') {
						wlStr = '理科';
					} else {
						wlStr = '综合';
					}
					$('#school_wl').html(wlStr);
					//三个圆环
					schoolDetails.radialIndicator(datal.guess, datal.mark, datal.chance);
					//折线图
					schoolDetails.eChartslLine(datal.lately);
					//学校概况
					schoolDetails.schoolIntroduce(datal);
					//招生简章
					GuideStr = '<li class="school-content">' + datal.jianzhang + '</li>';
					$('#schoolGuide').append(GuideStr);
					//就业信息
					EmpStr = '<li class="school-content">' + datal.employment + '</li>';
					$('#schoolEmployment').append(EmpStr);
					loadingMask.parentNode.removeChild(loadingMask);
				}else{
					loadingMask.parentNode.removeChild(loadingMask);
					fnBase.toast('暂无数据','50%');
				}
				 	
			},
			error: function() {
				 	loadingMask.parentNode.removeChild(loadingMask);
				fnBase.toast('暂无数据','50%');
			}
		});
	},
	//获取专业最低分
	getMajorList: function(page) {
		var page = page;
		var setData = {
			'user_id': fnBase.splitURL()['user_id'],
			'id': fnBase.splitURL()['school_id'],
			'page': page
		};
		fnBase.commonAjax(fnBase.api.School_CollegeMajorScore, setData, function(data) {
			if(data.code == 0) {
				var datal = data.data;
				if(datal.length != 0) {
					schoolDetails.page++;
					var str = '',
						Batch = '';
					for(var i = 0; i < datal.length; i++) {
						if(datal[i].batch == '01') {
							Batch = '本科提前批';
						} else if(datal[i].batch == '11') {
							Batch = '本科一批';
						} else if(datal[i].batch == '12') {
							Batch = '本科二批';
						} else if(datal[i].batch == '13') {
							Batch = '本科三批';
						} else if(datal[i].batch == '123') {
							Batch = '本科二、三批';
						}
						str += '<li class="admission-li" data_id="' + datal[i].majorid + '"><span>' + datal[i].majorname + '</span><span>' + datal[i].score_avg + '</span><span>' + datal[i].score_high + '</span><span>' + Batch + '</span></li>';
					}
					$('#admission_list').append(str);
				} else {
					$('#admission_more').remove();
				}
			}
		});
	},
	//三个圆
	radialIndicator: function(guess, mark, chance) {
		var guess = guess =='' ? 0 : parseInt(guess);
		var mark =  mark =='' ? 0 : parseInt(mark);
		var chance=chance ==''?0 :parseInt(chance);
		//var chance=parseInt(forecast);
		
		var predictionValue = radialIndicator('#predictionValue', {
			initValue: 0,
			barColor: '#FA4846',
			roundCorner: true,
			frameTime: 10, //时针的速度
			frameNum: 50,
			maxValue: 750,

		});
		predictionValue.animate(guess);
		var myValue = radialIndicator('#myValue', {
			initValue: 0,
			roundCorner: true,
			barColor: '#FA4846',
			frameTime: 10, //时针的速度
			frameNum: 50,
			maxValue: 750,

		});
		myValue.animate(mark);
		var myProbability = radialIndicator('#myProbability', {
			initValue: 0,
			barColor: '#FA4846',
			roundCorner: true,
			frameTime: 10, //时针的速度
			frameNum: 50,
			maxValue: 100,
			fontSize: 40,
			format: '##%',
		});
		myProbability.animate(chance);

	},
	TabStick: function() { //TAB置顶
		var myContBox=$('#school_content_wrap .school-TabStick');
		$('#school_nav .school_nav_children').click(function() {
			var _this=$(this);
			goThisTop(_this);
			_this.addClass('active').siblings().removeClass('active');
		});
		$('#makeng .makeng_children').click(function() {
			var _this=$(this);
			goThisTop(_this);
			_this.addClass('ma-active').siblings().removeClass('ma-active');
		});
		function goThisTop(_this){
			var _this=_this;
			var $index = _this.index();
			var $top = myContBox.eq($index).offset().top-_this.height();
			window.scrollTo(0, $top);
		}
		var makeng = document.getElementById('makeng');
		window.onscroll = function() {
			var bodyScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
			var myMakeng=$(makeng);
			if(bodyScrollTop > 500) {
				myMakeng.addClass('bodyUp');
			} else {
				myMakeng.removeClass('bodyUp');
			}
		};
//			function getTop(e) {
//				var offset = e.offsetTop;
//				if(e.offsetParent != null) offset += getTop(e.offsetParent);
//				return offset;
//			};		
	},
	//学校概况
	schoolIntroduce: function(datal) {
		var datal = datal;
		var str = '';
		var oDocdtor=datal.doctor==''?'':'博士点'+datal.doctor+'个,';
		var oSmaster=datal.smaster==''?'':'硕士点'+datal.smaster+'个';
		str = '<li class="content-title">学校地址</li>' +
			'<li class="school-content">' + datal.address + '</li>' +
			'<li class="content-title">联系电话</li>' +
			'<li class="school-content">' + datal.tel + '</li>' +
			'<li class="content-title">官方网站</li>' +
			'<li class="school-content">' + datal.website + '</li>' +
			'<li class="content-title">博士硕士点</li>' +
			'<li class="school-content">' + oDocdtor  + oSmaster + '</li>' +
			'<li class="content-title">重点学科</li>' +
			'<li class="school-content">' + datal.important + '</li>' +
			'<li class="content-title">建校历史</li>' +
			'<li class="school-content">' + datal.history + '</li>' +
			'<li class="content-title">学校简介</li>' +
			'<li class="school-content">' + datal.intro + '</li>';
		//console.log(datal.grad_capacity)
		$('#schoolIntroduce').append(str);
	},
	eChartslLine: function(lately) { //折线图
		//console.log(lately);
		lately.reverse();
		var myData = [];
		var myscore_avg = [];
		//{coord: [0, 683]}
		for(var i in lately) {
			myscore_avg.push(lately[i].score_avg);
			myData.push(lately[i].syear + '年');
		
		}
	
		// 基于准备好的dom，初始化echarts实例
		var myChart = echarts.init(document.getElementById('main'));
		// 指定图表的配置项和数据
		var option = {
			title: {},
			tooltip: {
				trigger: 'axis'
			},
			legend: {},
			grid: { //CANVAS 距离
				top: '8%',
				left: '5%',
				right: '5%',
			},
			xAxis: {
				type: 'category',
				//boundaryGap: false,
				axisTick: {
					show: false
				},
				axisLabel: { //字体颜色
					show: true,
					textStyle: {
						color: '#747474'
					}
				},
				axisLine: { //坐标轴颜色
					lineStyle: {
						color: '#e6e6e6',
					},
				},
				data: myData, //下面年份
			},
			yAxis: {
				axisLabel: false, //Y轴隐藏
				//axisLine:false,
				type: 'value',
				axisLine: {
					lineStyle: {
						color: '#e6e6e6'
					}
				},
				axisTick: {
					show: false
				},
				scale: true,
				precision: 2,
				splitNumber: 6,
				boundaryGap: [1, 0.8],
				splitArea: {
					show: false
				},
			},
			series: [{
				name: '录取分数',
				type: 'line',
				data: myscore_avg,
				smooth: true, //连接线原话
				itemStyle: {
					normal: {
						color: '#ff5e03'
					}
				},
				markPoint: {
					data: [{
						'coord': [0, myscore_avg[0]]
					}, {
						'coord': [1, myscore_avg[1]]
					}, {
						'coord': [2, myscore_avg[2]]
					}, {
						'coord': [3, myscore_avg[3]]
					}, ]
				},
			}, ]
		};
		// 使用刚指定的配置项和数据显示图表。
		myChart.setOption(option);
		//window.onresize = myChart.resize;
	},
}
$(document).ready(function() {
	schoolDetails.init();
	
});